//
//  YFCameraProgress.m
//  CamerDemo
//
//  Created by Winny Zhang on 2019/3/30.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFCameraProgress.h"

@interface YFCameraProgress ()

/**
 当前进度
 */
@property (nonatomic, assign) CGFloat currentProgress;

/**
 当前时间
 */
@property (nonatomic, assign) CGFloat currentTime;

@end

@implementation YFCameraProgress

- (void)drawRect:(CGRect)rect {
    
    // 开启上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 获取原点
    CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    // 半径
    CGFloat radius = self.frame.size.width * 0.5;
    // 起点
    CGFloat startAngle = -M_PI_2;
    // 终点
    CGFloat endAngle = -M_PI_2 + M_PI * 2 * _currentProgress;
    
    // 顺时针
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    CGContextSetLineWidth(ctx, 10);
    [[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1] setStroke];
    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}


#pragma mark - Set
- (void)setTotalProgress:(NSInteger)totalProgress {
    _totalProgress = totalProgress;
    self.currentProgress = 0;
    self.currentTime = 0;
    [self setNeedsDisplay];
    self.hidden = NO;
    [self performSelector:@selector(processRendering) withObject:nil afterDelay:0.1];
}


#pragma mark - 执行持续渲染方法
- (void)processRendering {
    _currentTime += 0.1;
    if (_totalProgress > _currentTime) {
        _currentProgress = _currentTime / _totalProgress;
        [self setNeedsDisplay];
        [self performSelector:@selector(processRendering) withObject:nil afterDelay:0.1];
    } else {
        
    }
}
#pragma mark - 重置
- (void)restore {
    _currentTime = _totalProgress;
    self.hidden = YES;
}


@end
